//
//  GXSuggestImageViewTableViewCell.m
//  GxLanOuProject
//
//  Created by 甘霞 on 15/10/26.
//  Copyright © 2015年 甘霞. All rights reserved.
//

#import "GXSuggestImageViewTableViewCell.h"
#import "NSString+GXSizeWithText.h"
#import "GXSuggestModel.h"
#import "UIImageView+WebCache.h"
#import "UIImage+GIF.h"

#define kBigImageViewWidth self.contentView.frame.size.width
#define kBigImageViewHeight 350.f

#define kBigImageViewSideBorder 15.f
#define kBigImageViewCellButtomBorder 25.f

#define kBigImageViewSpace 20.f
#define kBigImageViewBtnSize 35.f

#define kLabelSideBorder 40.f

#define kNameLabelWidth 150.f

@interface GXSuggestImageViewTableViewCell ()

@property (nonatomic, retain) UIImageView *bigImageView;
@property (nonatomic, retain) UILabel *titleLabel;
@property (nonatomic, retain) UILabel *nameLabel;
@property (nonatomic, retain) UILabel *typeLabel;

@end

@implementation GXSuggestImageViewTableViewCell

#pragma mark - 初始化控件
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self createSubviews];
    }
    return self;
}

#pragma mark - 创建子控件
- (void)createSubviews {
    //大图
    self.bigImageView = [[UIImageView alloc] init];
    [self.contentView addSubview:self.bigImageView];
    self.bigImageView.contentMode = UIViewContentModeScaleAspectFill;
    self.bigImageView.layer.masksToBounds = YES;
    
    //标题
    self.titleLabel = [[UILabel alloc] init];
    [self.titleLabel setNumberOfLines:0];
    [self.titleLabel setTextColor:[UIColor whiteColor]];
    [self.titleLabel setFont:[UIFont systemFontOfSize:kSuggestPoetryTitleFontSize]];
    [self.contentView addSubview:self.titleLabel];
    
    //姓名
    self.nameLabel = [[UILabel alloc] init];
    [self.contentView addSubview:self.nameLabel];
    [self.nameLabel setFont:[UIFont systemFontOfSize:kSuggestAuthorFontSize]];
    [self.nameLabel setTextColor:[UIColor whiteColor]];
    [self.nameLabel setTextAlignment:NSTextAlignmentRight];
    
    //类型
    self.typeLabel = [[UILabel alloc] init];
    [self.typeLabel setTextColor:[UIColor whiteColor]];
    [self.typeLabel setFont:[UIFont systemFontOfSize:13]];
    [self.typeLabel setTextAlignment:NSTextAlignmentRight];
    [self.contentView addSubview:self.typeLabel];
}

#pragma mark - 布局
- (void)layoutSubviews {
    [super layoutSubviews];
    
    CGFloat cellButtonY = kBigImageViewHeight - kBigImageViewCellButtomBorder - kBigImageViewBtnSize;
    
    //大图
    [self.bigImageView setFrame:CGRectMake(0, 0, kBigImageViewWidth, kBigImageViewHeight)];
    
    //标题
    CGRect titleLabelHeight = [self.titleLabel.text SizeWithTextFontSize:20 textWidth:kBigImageViewWidth - kLabelSideBorder];
    [self.titleLabel setFrame:CGRectMake(0, 0, kBigImageViewWidth -  kLabelSideBorder, titleLabelHeight.size.height)];
    self.titleLabel.center = CGPointMake(self.bigImageView.center.x, self.bigImageView.center.y - 30);
    
    //姓名
    CGFloat nameLabelY = CGRectGetMaxY(self.titleLabel.frame) + kBigImageViewSideBorder;
    [self.nameLabel setFrame:CGRectMake(kLabelSideBorder, nameLabelY, kBigImageViewWidth - 2 * kLabelSideBorder, kBigImageViewBtnSize)];
    
    //类型
    [self.typeLabel setFrame:CGRectMake(kBigImageViewWidth - kNameLabelWidth - kBigImageViewSideBorder, cellButtonY, kNameLabelWidth, kBigImageViewBtnSize)];
}

- (void)setSuggestModel:(GXSuggestModel *)suggestModel {
    if (_suggestModel != suggestModel) {
        _suggestModel = suggestModel;
    }
    
    //大图
    [self.bigImageView sd_setImageWithURL:_suggestModel.thumb[@"raw"] placeholderImage:[UIImage sd_animatedGIFNamed:@"loading"]];
    //标题
    self.titleLabel.text = _suggestModel.content[@"desc"];
    //姓名
    self.nameLabel.text = _suggestModel.content[@"signature"];
    //类型
    self.typeLabel.text = _suggestModel.title;
}

- (void)setCoreSugModel:(SuggestModel *)coreSugModel
{
    if (_coreSugModel != coreSugModel) {
        _coreSugModel = coreSugModel;
    }
    [self.bigImageView sd_setImageWithURL:[NSURL URLWithString:coreSugModel.share_image]];
    self.titleLabel.text = coreSugModel.type_name;
    self.nameLabel.text = [[NSString alloc] initWithData:coreSugModel.content encoding:NSUTF8StringEncoding];
    self.typeLabel.text = coreSugModel.title;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
